package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.invoice;

import android.app.Activity;
import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.ReceiptBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import androidx.lifecycle.MutableLiveData;

public class ReceiptListMode extends BaseViewMode {

    private MutableLiveData<ReceiptBean> liveResult;

    public MutableLiveData<ReceiptBean> getLiveData() {
        if (liveResult == null)
            liveResult = new MutableLiveData<>();
        return liveResult;
    }

    private MutableLiveData<String> live;

    public MutableLiveData<String> getLive() {
        if (live == null)
            live = new MutableLiveData<>();
        return live;
    }

    private MutableLiveData<Integer> liveCode;

    public MutableLiveData<Integer> getLiveCode() {
        if (liveCode == null)
            liveCode = new MutableLiveData<>();
        return liveCode;
    }

    /**
     * 根据uuid查询可开加油订单列表
     *
     * @param context
     * @param cardType
     * @param timeOutStatus
     * @param uuid
     */
    public void getUUIDOrderData(Context context, String cardType, String timeOutStatus, String uuid) {
        RequestUtils.GetUnInvoiceOrdersByUuid(context, cardType, timeOutStatus, uuid, new MyObserver<ReceiptBean>(context) {
            @Override
            public void onSuccess(ReceiptBean result) {
                liveResult.postValue(result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }

    public void getTokens(Context context, String money, String uuid) {
        getToken(context, token -> {
            getCoumosterPrice(context, money, token, uuid);
        });
    }

    private void getCoumosterPrice(Context context, String money, String tokens, String uuid) {
        String amountStr = new DecimalFormat("0").format(Double.valueOf(money) * 100);
        RequestUtils.DrawBillByCustomAmount(context, amountStr, DataUtil.getInstance().getCompanyId(), uuid, tokens, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    if (!StringUtils.isEmpty(errorMsg)) {
                        String totalMoney = ConverterHelper.toBigDecimal(errorMsg).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                        live.postValue(totalMoney);
                    }
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), errorMsg);
                }
            }
        });


    }


    public void getDrawBillTokens(Activity context, String subStr) {
        getToken(context, token -> {
            DrawBill(context, subStr, token);
        });
    }


    /**
     * 开具发票接口
     *
     * @param context
     * @param result
     * @param token
     */
    public void DrawBill(Activity context, String result, String token) {
        RequestUtils.DrawBill(context, result, token, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {
                liveCode.postValue(200);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    liveCode.postValue(200);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), errorMsg);
                }
            }
        });
    }
}
